<template>
  <a-row
    class="grid-demo"
    style="margin-bottom: 16px"
    align="center"
    :wrap="false"
  >
    <a-col flex="auto">
      <a-menu
        mode="horizontal"
        :selected-keys="selectedKeys"
        @menu-item-click="changeRoute"
      >
        <a-menu-item
          key="0"
          :style="{ padding: 0, marginRight: '38px' }"
          disabled
        >
          <div class="title-bar">
            <img src="@/assets/rose.svg" width="50px" />
            <text class="title">OJ</text>
          </div>
        </a-menu-item>
        <a-menu-item :key="item.path" v-for="item in visibleRoutes">
          {{ item.name }}
        </a-menu-item>
      </a-menu>
    </a-col>
    <a-col flex="100px">
      <div>
        <a-dropdown :popup-max-height="false">
          <a-avatar
            :style="{
              backgroundColor:
                userInfo.userRole !== ACCESS_ENUM.NOT_LOGIN ? '#14a9f8' : '',
            }"
            :size="48"
            >{{ userInfo.userName }}
          </a-avatar>
          <template #content>
            <a-doption @click="stateOperation">
              <template #icon>
                <icon-import />
              </template>
              <template #default>
                {{
                  userInfo.userRole === ACCESS_ENUM.NOT_LOGIN
                    ? "登录/注册"
                    : "注销"
                }}
              </template>
            </a-doption>
          </template>
        </a-dropdown>
      </div>
    </a-col>
  </a-row>
</template>

<script setup lang="ts">
import { useRouter } from "vue-router";
import { computed, ref } from "vue";
import { routes } from "@/router/routes";
import { useStore } from "vuex";
import CheckAccess from "@/access/checkAccess";
import { UserControllerService } from "../../generated";
import ACCESS_ENUM from "@/access/accessEnum";
import message from "@arco-design/web-vue/es/message";

const store = useStore();
const router = useRouter();
const selectedKeys = ref([window.location.pathname]);
const userInfo = computed(() => store.state.user.userInfo);

/**
 * 切换路由，根据路由切换页面
 * @param key 路由
 */
const changeRoute = (key: string) => {
  selectedKeys.value = [key];
  router.push({
    path: key,
  });
};

/**
 * 过滤路由列表，获取可访问路由对应的组件
 */
const visibleRoutes = computed(() =>
  routes.filter((item) => {
    if (
      userInfo.value.userRole !== ACCESS_ENUM.NOT_LOGIN &&
      item.path == "/login"
    ) {
      return false;
    }
    if (item.meta?.hideInMenu) {
      //隐藏菜单
      return false;
    }
    //没有权限
    return CheckAccess(userInfo.value, item?.meta?.access as string);
  })
);

/**
 * 注销操作和登陆操作
 */
const stateOperation = async () => {
  if (userInfo.value.userRole === ACCESS_ENUM.NOT_LOGIN) {
    router.push("/login");
  } else {
    const res = await UserControllerService.userLogoutUsingPost();
    if (res.code === 0) {
      store.dispatch("getLoginUser");
      router.push("/");
    } else {
      message.error("注销失败，" + res.message);
    }
  }
};
</script>
<style scoped>
.title-bar {
  display: flex;
  align-items: center;
  gap: 3px;
}

.title {
  font-size: 20px;
  color: black;
}
</style>
